﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using Telerik.Web.UI;
using Telerik.Charting;
using System.IO;
using SmartLib;
//NODE
/*xoa mact ben table giamsatnuocthai
  them vao id(int tu tang) ben table Tinhthongso
  them ThuocKCN(bit) vao table congxa
  doi keu du lieu luuluong va nongdo thanh float ben giamsatnuocthai */
public partial class Giamsatdinhky_Tinhthongso : System.Web.UI.Page
{
    string _id = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Employee"] == null && Session["Admin"] == null)
            Response.Redirect("../login.aspx");
        if (!Page.IsPostBack)
        {
            RadComboBoxRadComboBoxDotGiamSatId_Load(null, null);
        }

    }
    protected void RadComboBoxRadComboBoxDotGiamSatId_Load(object sender, EventArgs e)
    {
        string sql = "SELECT TOP 20 * from DotGiamSat";
        DataTable myData = Helper.GetDataTable(sql);
        RadComboBoxDotGiamSatId.DataSourceID = "";
        RadComboBoxDotGiamSatId.DataTextField = "Dotgiamsat";
        RadComboBoxDotGiamSatId.DataValueField = "Madotgs";
        RadComboBoxDotGiamSatId.DataSource = myData;
        RadComboBoxDotGiamSatId.DataBind();
    }
    protected void TinhThongSoONhiem()
    {
        DataTable myData0 = Helper.GetDataTable("Select * from Chitieunuoc");
        Helper.ExecuteNonQuery("Delete from Tinhthongso WHERE Madotgs=" + RadComboBoxDotGiamSatId.SelectedValue, Helper.getConnectionString());
        for (int ii = 0; ii < myData0.Rows.Count; ii++)//duyet tung chi tieu
        {
            DataTable myData = Helper.GetDataTable("Select * from Congxa");
            for (int i = 0; i < myData.Rows.Count; i++)//duyet tung cong xa~
            {
                //la khu cong nghiep
                int Error = 0;
                if (!Helper.checkExist("Tinhthongso", ("Madotgs=" + RadComboBoxDotGiamSatId.SelectedValue + " AND Macong=" + myData.Rows[i]["Macong"] + " AND mact=" + myData0.Rows[ii]["mact"]), out Error))
                {
                    if ((bool)myData.Rows[i]["ThuocKCN"])
                    {
                        DataTable myData2 = Helper.GetDataTable("Select * FROM vw_tbl_GiamSatNuocThai_KhuCongNghiep WHERE Makcn=" + myData.Rows[i]["Makcn"].ToString() + " AND Madotgs=" + RadComboBoxDotGiamSatId.SelectedValue);
                        double luuluong = 0;
                        double tongluuluong = 0;
                        double tonggiatrichitieu = 0;
                        //quet het cac giam sat cua cac doanh nghiep
                        for (int j = 0; j < myData2.Rows.Count; j++)
                        {
                            try
                            {

                                //lay chi tieu tuong ung trong dot giam sat nay
                                DataTable myData3 = Helper.GetDataTable("Select * from Chitietgs WHERE Mags=" + myData2.Rows[j]["Mags"].ToString() + " AND mact=" + myData0.Rows[ii]["mact"].ToString());
                                if (myData3.Rows.Count > 0)
                                {
                                    try
                                    {
                                        if (myData3.Rows[0]["nongdo"] != null && myData3.Rows[0]["nongdo"].ToString() != "" && Convert.ToDouble(myData3.Rows[0]["nongdo"]) != 0)
                                        {
                                            tonggiatrichitieu += (luuluong * Convert.ToDouble(myData3.Rows[0]["nongdo"]));
                                            //neu co do dac moi cong luu luong vao
                                            luuluong = Convert.ToDouble(myData2.Rows[j]["Luuluong"]);
                                            tongluuluong += luuluong;
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                    }
                                }
                            }
                            catch (Exception ex) { }
                        }
                        if (tongluuluong > 0 && tonggiatrichitieu > 0)
                        {
                            //them gia tri nay vao CSDL
                            tbl_KetQuaTinhToan obj = new tbl_KetQuaTinhToan();
                            obj.mChiTieu = Convert.ToInt32(myData0.Rows[ii]["mact"]);
                            obj.mCongXaId = Convert.ToInt32(myData.Rows[i]["Macong"]);
                            obj.mDotGiamSatId = Convert.ToInt32(RadComboBoxDotGiamSatId.SelectedValue);
                            obj.mLuuLuong = tongluuluong;
                            obj.mNongDo = tonggiatrichitieu / tongluuluong;
                            Connection.Add_tbl_KetQuaTinhToan(obj);

                        }
                    }

                    else//la doanh nghiep
                    {
                        DataTable myData2 = Helper.GetDataTable("Select * from Giamsatnuocthai WHERE Madnghiep=" + myData.Rows[i]["Madnghiep"].ToString() + " AND Madotgs=" + RadComboBoxDotGiamSatId.SelectedValue);
                        double luuluong = 0;
                        if (myData2.Rows.Count > 0)
                        {
                            try
                            {
                                luuluong = Convert.ToDouble(myData2.Rows[0]["Luuluong"]);
                                DataTable myData3 = Helper.GetDataTable("Select * from Chitietgs WHERE Mags=" + myData2.Rows[0]["Mags"].ToString() + " AND mact=" + myData0.Rows[ii]["mact"].ToString());
                                if (myData3.Rows.Count > 0)
                                {
                                    //them gia tri nay vao CSDL

                                    tbl_KetQuaTinhToan obj = new tbl_KetQuaTinhToan();
                                    obj.mChiTieu = Convert.ToInt32(myData0.Rows[ii]["mact"]);
                                    obj.mCongXaId = Convert.ToInt32(myData.Rows[i]["Macong"]);
                                    obj.mDotGiamSatId = Convert.ToInt32(RadComboBoxDotGiamSatId.SelectedValue);
                                    obj.mLuuLuong = luuluong;
                                    obj.mNongDo = Convert.ToDouble(myData3.Rows[0]["nongdo"]);
                                    Connection.Add_tbl_KetQuaTinhToan(obj);

                                }
                            }
                            catch (Exception ex)
                            {
                            }
                        }
                    }
                }
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        TinhThongSoONhiem();
        createHeader();
        createBody();
        createFooter();
    }
    protected void chkTenShowOnReport_CheckedChanged(object sender, EventArgs e)
    {
        createHeader();
        createBody();
        createFooter();
    }
    protected void chkXShowOnReport_CheckedChanged(object sender, EventArgs e)
    {
        createHeader();
        createBody();
        createFooter();
    }
    protected void chkYShowOnReport_CheckedChanged(object sender, EventArgs e)
    {
        createHeader();
        createBody();
        createFooter();
    }
    protected void chkLuuLuongShowOnReport_CheckedChanged(object sender, EventArgs e)
    {
        createHeader();
        createBody();
        createFooter();
    }
    protected void chkChiTieuShowOnReport_CheckedChanged(object sender, EventArgs e)
    {
        createHeader();
        createBody();
        createFooter();
    }
    protected void chkNongDoShowOnReport_CheckedChanged(object sender, EventArgs e)
    {
        createHeader();
        createBody();
        createFooter();
    }
    private void createHeader()
    {
        divHeader.InnerHtml = "<span style=\"color: #000ffa\"><h2>TÍNH THÔNG SỐ MÔ HÌNH</h2></span>";
       
    }
    private void createBody()
    {
        
        divBody.InnerHtml = "<div align=\"left\">";
        divBody.InnerHtml += "<table border=\"1\" cellpadding=\"5\" cellspacing=\"0\" style=\"border-collapse: collapse; border: 1px outset #CDCDCD\" width=\"100%\" bordercolorlight=\"#CDCDCD\">";
        divBody.InnerHtml += " <tr><td class=\"reporttd\" align=\"center\"><span style=\"color: #000ffa\"><b>STT</b></span></td>";
        string _Ten = "";
        bool _1 = true;
        double sumTen = 0;
        double countTen = 0;
        if (chkTenShowOnReport.Checked)
            divBody.InnerHtml += "   <td  class=\"reporttd\" align=\"center\"><span style=\"color: #000ffa\"><b>Tên cống xả</b></span></td>";
        string _X = "";
        bool _2 = true;
        double sumX = 0;
        double countX = 0;
        if (chkXShowOnReport.Checked)
            divBody.InnerHtml += "   <td  class=\"reporttd\" align=\"center\"><span style=\"color: #000ffa\"><b>Tọa độ X</b></span></td>";
      string _Y = "";
        bool _3 = true;
        double sumY = 0;
        double countY = 0;
        if (chkYShowOnReport.Checked)
            divBody.InnerHtml += "   <td  class=\"reporttd\" align=\"center\"><span style=\"color: #000ffa\"><b>Tọa độ Y</b></span></td>";
        string _LuuLuong = "";
        bool _4 = true;
        double sumLuuLuong = 0;
        double countLuuLuong = 0;
        if (chkLuuLuongShowOnReport.Checked)
            divBody.InnerHtml += "   <td  class=\"reporttd\" align=\"center\"><span style=\"color: #000ffa\"><b>Lưu lượng nước thải</b></span></td>";
        string _ChiTieu = "";
        bool _5 = true;
        double sumChiTieu = 0;
        double countChiTieu = 0;
        if (chkChiTieuShowOnReport.Checked)
            divBody.InnerHtml += "   <td  class=\"reporttd\" align=\"center\"><span style=\"color: #000ffa\"><b>Chỉ tiêu</b></span></td>";
        string _NongDo = "";
        bool _6 = true;
        double sumNongDo = 0;
        double countNongDo = 0;
        if (chkNongDoShowOnReport.Checked)
            divBody.InnerHtml += "   <td  class=\"reporttd\" align=\"center\"><span style=\"color: #000ff0\"><b>Nồng độ</b></span></td>";
           divBody.InnerHtml += "</tr>";
        string whStr = "";
        if (txtTen.Text != "")
        {
            if (whStr != "")
                whStr += " AND Tencong LIKE N'%" + txtTen.Text + "%'";
            else
                whStr = " WHERE Tencong LIKE N'%" + txtTen.Text + "%'";
        }
        if (txtChiTieu.Text != "")
        {
            if (whStr != "")
                whStr += " AND Tenchitieu LIKE N'%" + txtChiTieu.Text + "%'";
            else
                whStr = " WHERE Tenchitieu LIKE N'%" + txtChiTieu.Text + "%'";
        }
     /*   if (RadComboBoxDotGiamSatId.Text != "")
        {
            if (whStr != "")
                whStr += " AND Madotgs =" + RadComboBoxDotGiamSatId.SelectedValue + "";
            else
                whStr = " WHERE Madotgs =" + RadComboBoxDotGiamSatId.SelectedValue + "";
        }*/
        DataTable myData = Helper.GetDataTable("Select * from vw_rpt_KetQuaTinhToan" + whStr);
        for (int i = 0; i < myData.Rows.Count; i++)
        {
            divBody.InnerHtml += "   <tr ><td  align=\"center\">" + (i + 1) + "</td>";
            if (i == 0)
            {
                countTen = countTen + 1;
                _Ten = (myData.Rows[i]["Tencong"] == null) ? "" : myData.Rows[i]["Tencong"].ToString();
            }
            else
            {
                bool tmpB = false;
                if (!tmpB && _Ten == ((myData.Rows[i]["Tencong"] == null) ? "" : myData.Rows[i]["Tencong"].ToString()))
                    _1 = false;
                else
                {
                    countTen++;
                    _Ten = (myData.Rows[i]["Tencong"] == null) ? "" : myData.Rows[i]["Tencong"].ToString();
                    _1 = true;
                }
            }
            if (chkTenShowOnReport.Checked)
                if (_1)
                    divBody.InnerHtml += "<td bgcolor=\"ffffce\" align=\"left\"> " + myData.Rows[i]["Tencong"].ToString() + "</td>";
                else
                    divBody.InnerHtml += "<td align=\"left\"></td>";
            if (i == 0)
            {
                countX = countX + 1;
                _X = (myData.Rows[i]["ToadoX"] == null) ? "" : myData.Rows[i]["ToadoX"].ToString();
            }
            else
            {
                bool tmpB = false;
                if (_1 == true) tmpB = true;
                if (!tmpB && _X == ((myData.Rows[i]["ToadoX"] == null) ? "" : myData.Rows[i]["ToadoX"].ToString()))
                    _2 = false;
                else
                {
                    countX++;
                    _X = (myData.Rows[i]["ToadoX"] == null) ? "" : myData.Rows[i]["ToadoX"].ToString();
                    _2 = true;
                }
            }
            if (_1 == true) _2 = true;
            if (chkXShowOnReport.Checked)
                if (_2)
                    divBody.InnerHtml += "<td bgcolor=\"ffffce\" align=\"left\"> " + (myData.Rows[i]["ToadoX"] == null || myData.Rows[i]["ToadoX"].ToString() == "" ? "" : ((int)myData.Rows[i]["ToadoX"]).ToString("###,##0.####")) + "</td>";
                else
                    divBody.InnerHtml += "<td align=\"left\"></td>";
            if (i == 0)
            {
                countY = countY + 1;
                _Y = (myData.Rows[i]["ToadoY"] == null) ? "" : myData.Rows[i]["ToadoY"].ToString();
            }
            else
            {
                bool tmpB = false;
                if (_1 == true) tmpB = true;
                if (_2 == true) tmpB = true;
                if (!tmpB && _Y == ((myData.Rows[i]["ToadoY"] == null) ? "" : myData.Rows[i]["ToadoY"].ToString()))
                    _3 = false;
                else
                {
                    countY++;
                    _Y = (myData.Rows[i]["ToadoY"] == null) ? "" : myData.Rows[i]["ToadoY"].ToString();
                    _3 = true;
                }
            }
            if (_1 == true) _3 = true;
            if (_2 == true) _3 = true;
            if (chkYShowOnReport.Checked)
                if (_3)
                    divBody.InnerHtml += "<td bgcolor=\"ffffce\" align=\"left\"> " + (myData.Rows[i]["ToadoY"] == null || myData.Rows[i]["ToadoY"].ToString() == "" ? "" : ((int)myData.Rows[i]["ToadoY"]).ToString("###,##0.####")) + "</td>";
                else
                    divBody.InnerHtml += "<td align=\"left\"></td>";
            if (i == 0)
            {
                countLuuLuong = countLuuLuong + 1;
                _LuuLuong = (myData.Rows[i]["Luuluong"] == null) ? "" : myData.Rows[i]["Luuluong"].ToString();
            }
            else
            {
                bool tmpB = false;
                if (_1 == true) tmpB = true;
                if (_2 == true) tmpB = true;
                if (_3 == true) tmpB = true;
                if (!tmpB && _LuuLuong == ((myData.Rows[i]["Luuluong"] == null) ? "" : myData.Rows[i]["Luuluong"].ToString()))
                    _4 = false;
                else
                {
                    countLuuLuong++;
                    _LuuLuong = (myData.Rows[i]["Luuluong"] == null) ? "" : myData.Rows[i]["Luuluong"].ToString();
                    _4 = true;
                }
            }
            if (_1 == true) _4 = true;
            if (_2 == true) _4 = true;
            if (_3 == true) _4 = true;
            if (chkLuuLuongShowOnReport.Checked)
                if (_4)
                    divBody.InnerHtml += "<td bgcolor=\"ffffce\" align=\"left\"> " + (myData.Rows[i]["Luuluong"] == null || myData.Rows[i]["Luuluong"].ToString() == "" ? "" : ((double)myData.Rows[i]["Luuluong"]).ToString("###,##0.####")) + "</td>";
                else
                    divBody.InnerHtml += "<td align=\"left\"></td>";
            countChiTieu++;
            if (_1 == true) _5 = true;
            if (_2 == true) _5 = true;
            if (_3 == true) _5 = true;
            if (_4 == true) _5 = true;
            if (chkChiTieuShowOnReport.Checked)
                if (_5)
                    divBody.InnerHtml += "<td  align=\"left\"> " + myData.Rows[i]["Tenchitieu"].ToString() + "</td>";
                else
                    divBody.InnerHtml += "<td align=\"left\"></td>";
            countNongDo++;
            if (_1 == true) _6 = true;
            if (_2 == true) _6 = true;
            if (_3 == true) _6 = true;
            if (_4 == true) _6 = true;
            if (_5 == true) _6 = true;
            if (chkNongDoShowOnReport.Checked)
                if (_6)
                    divBody.InnerHtml += "<td  align=\"left\"> " + (myData.Rows[i]["nongdo"] == null || myData.Rows[i]["Luuluong"].ToString() == "" ? "" : ((double)myData.Rows[i]["nongdo"]).ToString("###,##0.####")) + "</td>";
                else
                    divBody.InnerHtml += "<td align=\"left\"></td>";
            divBody.InnerHtml += "</tr>";
        }
        divBody.InnerHtml += " <tr><td bgcolor=\"#C0C0C0\" align=\"center\"><b>Tổng cộng</b></td>";
        if (chkTenShowOnReport.Checked)
            divBody.InnerHtml += "   <td  bgcolor=\"#C0C0C0\" align=\"center\"><b>" + countTen + "</b></td>";
        if (chkXShowOnReport.Checked)
            divBody.InnerHtml += "<td bgcolor=\"#C0C0C0\" align=\"center\"></td>";
        if (chkYShowOnReport.Checked)
            divBody.InnerHtml += "<td bgcolor=\"#C0C0C0\" align=\"center\"></td>";
        if (chkLuuLuongShowOnReport.Checked)
            divBody.InnerHtml += "<td bgcolor=\"#C0C0C0\" align=\"center\"></td>";
        if (chkChiTieuShowOnReport.Checked)
            divBody.InnerHtml += "<td bgcolor=\"#C0C0C0\" align=\"center\"></td>";
        if (chkNongDoShowOnReport.Checked)
            divBody.InnerHtml += "<td bgcolor=\"#C0C0C0\" align=\"center\"></td>";
        divBody.InnerHtml += "</tr>";
        divBody.InnerHtml += "</table>";
        divBody.InnerHtml += "</div>";
    }
    private void createFooter()
    {
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        ExportWord();
    }
    private void ExportWord()
    {
        string attachment = "";
        if (exportType.SelectedValue == "doc")
            attachment = "attachment;filename=rpt_vw_rpt_KetQuaTinhToan.doc";
        else
            attachment = "attachment;filename=rpt_vw_rpt_KetQuaTinhToan.xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        if (exportType.SelectedValue == "doc")
            Response.ContentType = "application/ms-doc; charset=UTF-8";
        else
            Response.ContentType = "application/ms-excel; charset=UTF-8";
        StringWriter sw = new StringWriter();
        sw.WriteLine(divHeader.InnerHtml);
        sw.WriteLine(divBody.InnerHtml);
        sw.WriteLine(divFooter.InnerHtml);
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        Response.Write(sw.ToString());
        Response.End();
    }

}
